using MicroMsg.Common.Event;
using MicroMsg.Manager;
using MicroMsg.Plugin;
using MicroMsg.Protocol;
using MicroMsg.Resource.@string;
using MicroMsg.Scene;
using MicroMsg.Storage;
using MicroMsg.UI.Page;
using MicroMsg.UI.UserContrl;
using MicroMsg.UI.Util;
using Microsoft.Phone.Controls;
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace MicroMsg.Source.UI.AddFriends
{
	public class AddFriendBy : BasePage
	{
		private EventWatcher m_WatcherFindFriendsErr;
		private EventWatcher m_WatcherFindFriendsSuccess;
		private AddContactScene searchType;
		internal Grid LayoutRoot;
		internal panelTitle2Line panelTitle;
		internal Grid BindQQTabLayout;
		internal TextBlock textBlockFriendAccount;
		internal PhoneTextBox textBoxFriendAccount;
		private bool _contentLoaded;
		public AddFriendBy()
		{
			this.InitializeComponent();
			base.add_Loaded(new RoutedEventHandler(this.initUI));
		}
		private void initUI(object sender, RoutedEventArgs e)
		{
			this.textBoxFriendAccount.Focus();
		}
		protected override void OnPageIn(object[] object_0)
		{
			this.m_WatcherFindFriendsErr = new EventWatcher(this, null, new EventHandlerDelegate(this.onEventHandlerFindFriendsErr));
			this.m_WatcherFindFriendsSuccess = new EventWatcher(this, null, new EventHandlerDelegate(this.onEventHandlerFindFriendsSucess));
			EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_SEARCHCONTACT_ERR, this.m_WatcherFindFriendsErr);
			EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_SEARCHCONTACT_SUCCESS, this.m_WatcherFindFriendsSuccess);
			this.hideProgressCover();
		}
		protected override void OnPageOut()
		{
			if (this.m_WatcherFindFriendsErr != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_SEARCHCONTACT_ERR, this.m_WatcherFindFriendsErr);
				this.m_WatcherFindFriendsErr = null;
			}
			if (this.m_WatcherFindFriendsSuccess != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_SEARCHCONTACT_SUCCESS, this.m_WatcherFindFriendsSuccess);
				this.m_WatcherFindFriendsSuccess = null;
			}
		}
		private bool TryGoToPluginSettingView(string pluginName)
		{
			string text = pluginName.ToLower();
			string text2;
			if ((text2 = text) != null)
			{
				if (text2 == "lbsapp")
				{
					ToolFunction.GotoPageForUserControl("/Source/Plugin/PluginSettingPage.xaml", new object[]
					{
						PluginManager.findPluginByName("Lbs")
					}, false);
					return true;
				}
				if (text2 == "shakeapp")
				{
					ToolFunction.GotoPageForUserControl("/Source/Plugin/PluginSettingPage.xaml", new object[]
					{
						PluginManager.findPluginByName("Shake")
					}, false);
					return true;
				}
				if (text2 == "floatbottle")
				{
					ToolFunction.GotoPageForUserControl("/Source/Plugin/PluginSettingPage.xaml", new object[]
					{
						PluginManager.findPluginByName("DriftBottle")
					}, false);
					return true;
				}
				if (text2 == "facebookapp")
				{
					ToolFunction.GotoPageForUserControl("/Source/Plugin/PluginSettingPage.xaml", new object[]
					{
						PluginManager.findPluginByName("Facebook")
					}, false);
					return true;
				}
				if (text2 == "newsapp")
				{
					ToolFunction.GotoPageForUserControl("/Source/Plugin/PluginSettingPage.xaml", new object[]
					{
						PluginManager.findPluginByName("TXNews")
					}, false);
					return true;
				}
			}
			return false;
		}
		private void textBoxFriendAccount_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.get_Key() == 3 && sender == this.textBoxFriendAccount)
			{
				e.set_Handled(true);
				this.DoSearchFriend();
				base.Focus();
			}
		}
		private void DoSearchFriend()
		{
			string text = this.textBoxFriendAccount.get_Text();
			Account curAccount = AccountMgr.getCurAccount();
			if (text == "")
			{
				MessageBoxManager.ShowToast(strings.FF_FindContactTip, 2500, null, false);
				return;
			}
			if (text == curAccount.strBindMobile || text == curAccount.strUsrName || text == curAccount.nBindQQ.ToString())
			{
				MessageBoxManager.ShowToast(strings.FF_SearchFriendCurUser, 2500, null, false);
				return;
			}
			if (this.TryGoToPluginSettingView(text))
			{
				return;
			}
			Contact contact = StorageMgr.contact.method_0(text);
			if (ToolFunction.checkIsLegalQQNum(text))
			{
				this.searchType = AddContactScene.MM_ADDSCENE_SEARCH_QQ;
			}
			else
			{
				if (ToolFunction.checkIsChinaNumber(text))
				{
					this.searchType = AddContactScene.MM_ADDSCENE_SEARCH_PHONE;
				}
				else
				{
					if (ToolFunction.checkIsMailAddress(text))
					{
						this.searchType = AddContactScene.MM_ADDSCENE_SEARCH_EMAIL;
					}
					else
					{
						this.searchType = AddContactScene.MM_ADDSCENE_SEARCH_WEIXIN;
					}
				}
			}
			if (contact == null)
			{
				ServiceCenter.sceneSearchContact.doScene(text);
				this.showProgressCover(strings.FF_SearchingFriend);
				return;
			}
			ContactDetailFromType contactFromType = this.GetContactFromType(this.searchType);
			ContactDetailViewForAll.GotoContactDitailView(text, contactFromType, false);
		}
		private void onEventHandlerFindFriendsErr(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs != null && evtArgs.mObject != null)
			{
				RetConst retConst = (RetConst)evtArgs.mObject;
				if (retConst == RetConst.MM_ERR_CLIENT)
				{
					MMMessageBox.Show(strings.Reg_PhoneV_MsgBadNet, true, "确定");
				}
				else
				{
					MMMessageBox.Show(strings.FF_SearchFriendNoUser, true, "确定");
				}
			}
			this.hideProgressCover();
		}
		private void onEventHandlerFindFriendsSucess(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			this.hideProgressCover();
			SearchContactInfo searchContactInfo = evtArgs.mObject as SearchContactInfo;
			ContactDetailFromType contactFromType = this.GetContactFromType(this.searchType);
			ContactDetailViewForAll.GotoContactDitailView(searchContactInfo, contactFromType, false);
		}
		private ContactDetailFromType GetContactFromType(AddContactScene addType)
		{
			switch (addType)
			{
			case AddContactScene.MM_ADDSCENE_SEARCH_QQ:
				return ContactDetailFromType.FROM_SEARCHQQ;
			case AddContactScene.MM_ADDSCENE_SEARCH_EMAIL:
				return ContactDetailFromType.FROM_SEARCHEMAIL;
			case AddContactScene.MM_ADDSCENE_SEARCH_WEIXIN:
				return ContactDetailFromType.FROM_SEARCHWEIXIN;
			default:
				if (addType != AddContactScene.MM_ADDSCENE_SEARCH_PHONE)
				{
					return ContactDetailFromType.FROM_SEARCHWEIXIN;
				}
				return ContactDetailFromType.FROM_SEARCHPHONE;
			}
		}
		private void showProgressCover(string strCoverTitle)
		{
			ProcessingCover.getProgressBar(this.LayoutRoot, strCoverTitle, null, false, false, null);
		}
		private void hideProgressCover()
		{
			ProcessingCover.releaseProgressBar(this.LayoutRoot);
		}
		private void textBoxFriendAccount_TextChanged(object sender, TextChangedEventArgs e)
		{
			if (string.IsNullOrEmpty(this.textBoxFriendAccount.get_Text()))
			{
				base.Focus();
				this.textBoxFriendAccount.Focus();
			}
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/UI/AddFriends/AddFriendBy.xaml", 2));
			this.LayoutRoot = (Grid)base.FindName("LayoutRoot");
			this.panelTitle = (panelTitle2Line)base.FindName("panelTitle");
			this.BindQQTabLayout = (Grid)base.FindName("BindQQTabLayout");
			this.textBlockFriendAccount = (TextBlock)base.FindName("textBlockFriendAccount");
			this.textBoxFriendAccount = (PhoneTextBox)base.FindName("textBoxFriendAccount");
		}
	}
}
